package com.yu1996.gulimall.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu1996.common.constant.MemberConstant;
import com.yu1996.common.utils.PageUtils;
import com.yu1996.common.utils.Query;
import com.yu1996.gulimall.member.dao.MemberDao;
import com.yu1996.gulimall.member.dao.MemberLevelDao;
import com.yu1996.gulimall.member.entity.MemberEntity;
import com.yu1996.gulimall.member.entity.MemberLevelEntity;
import com.yu1996.gulimall.member.exception.PhoneExistException;
import com.yu1996.gulimall.member.exception.UsernameExistException;
import com.yu1996.gulimall.member.service.MemberService;
import com.yu1996.gulimall.member.vo.UserLoginVo;
import com.yu1996.gulimall.member.vo.UserRegisterVo;
import java.util.Date;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity>
    implements MemberService {

  @Autowired private MemberDao memberDao;

  @Autowired private MemberLevelDao memberLevelDao;

  @Override
  public PageUtils queryPage(Map<String, Object> params) {
    IPage<MemberEntity> page =
        this.page(new Query<MemberEntity>().getPage(params), new QueryWrapper<MemberEntity>());

    return new PageUtils(page);
  }

  /**
   * 会员注册
   *
   * @param userRegisterVo
   */
  @Override
  public void regist(UserRegisterVo userRegisterVo) throws PhoneExistException,UsernameExistException{

    MemberEntity memberEntity = new MemberEntity();
    memberEntity.setCreateTime(new Date());

    // 查询默认会员等级
    MemberLevelEntity memberLevelEntity =
        memberLevelDao.getDefaultLevel(MemberConstant.Default_Membership_Level);
    memberEntity.setLevelId(memberLevelEntity.getId());
    memberEntity.setNickname(userRegisterVo.getUsername());
    // 检查用户名和手机号是否已经存在   ---采用异常机制给上层抛错误信息
    checkPhoneUnique(userRegisterVo.getPhone());
    checkUsernameUnique(userRegisterVo.getUsername());
    memberEntity.setUsername(userRegisterVo.getUsername());
    memberEntity.setMobile(userRegisterVo.getPhone());
    // 设置密码  ---采用md5+盐 加密方式(SpringSecurity封装的加密工具类)
    BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
    memberEntity.setPassword(passwordEncoder.encode(userRegisterVo.getPassword()));
    memberDao.insert(memberEntity);
  }

  /**
   * 检查手机号是否存在
   *
   * @param phone
   */
  @Override
  public void checkPhoneUnique(String phone) throws PhoneExistException{
    Integer mobile = memberDao.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));
    if (mobile > 0) {
      throw new PhoneExistException();
    }
  }

  /**
   * 检查用户名是否存在
   *
   * @param username
   */
  @Override
  public void checkUsernameUnique(String username) throws UsernameExistException{
    Integer name = memberDao.selectCount(new QueryWrapper<MemberEntity>().eq("username", username));
    if (name > 0) {
      throw new UsernameExistException();
    }
  }

  /**
   * 用户登录 ---member
   *
   * @param userLoginVo
   * @return
   */
  @Override
  public MemberEntity login(UserLoginVo userLoginVo) {
    // 先按照用户名查询账号是否存在
    MemberEntity memberEntity =
        memberDao.selectOne(
            new QueryWrapper<MemberEntity>()
                .eq("username", userLoginVo.getLoginAcct())
                .or()
                .eq("mobile", userLoginVo.getLoginAcct()));
    if (memberEntity != null) {
      //账号存在  看密码是否匹配
      BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
      boolean matches =
          passwordEncoder.matches(userLoginVo.getPassword(), memberEntity.getPassword());
      if (matches) {
        // 账号密码都匹配   登录成功返回用户
        return memberEntity;
      } else {
        // 密码不匹配
        return null;
      }
    }
    return null;
  }
}
